﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using ContactManager.Common;

namespace ContactManager
{
    public partial class ContactFrm : AlSkin.AlForm.AlBaseForm
    {
        public ContactFrm()
        {
            InitializeComponent();
        }

        public void setDataGridView(int id, string dname, string phone, string dcphone, string dcompany)
        {
            this.dataGridViewContacter.Rows.Add(id, dname, phone, dcphone, dcompany);
        }

        public void clearDataGridView()
        {
            this.dataGridViewContacter.Rows.Clear();
        }

        private void ContactFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void dataGridViewContacter_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridViewContacter.CurrentRow != null)
            {
                TracesForm tf = new TracesForm(dataGridViewContacter.CurrentRow.Cells["ContactID"].Value.ToString());
                tf.Show();
            }
        }

        private void DelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string delSql = "delete * from contacters where c_id =" + dataGridViewContacter.CurrentRow.Cells["ContactID"].Value.ToString();
            if (DialogResult.Yes == MessageBox.Show("确实要删除此联系人吗？此操作会删除其所有追踪记录。", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
            {
                if (AccessHelper.ExecuteNonQuery(delSql) > 0)
                {
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadAll();
                }
                else
                {
                    MessageBox.Show("删除失败，请重试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

        private void loadAll()
        {
            string sql = "select t.c_id,t.c_name,t.c_phone_one,t.c_phone_second,t.c_company_phone,t.c_company from contacters t order by t.c_id desc";
            DataSet ds = AccessHelper.ExecuteQuery(sql);
            this.dataGridViewContacter.Rows.Clear();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                int id = Convert.ToInt32(dr["c_id"].ToString());
                string name = dr["c_name"].ToString();
                string phone1 = dr["c_phone_one"].ToString();
                string phone2 = dr["c_phone_second"].ToString();
                string cphone = dr["c_company_phone"].ToString();
                string company = dr["c_company"].ToString();
                setDataGridView(id, name, phone1 + "/" + phone2, cphone, company);
            }
        }

        private void DetailToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.dataGridViewContacter_DoubleClick(this, null);
        }

        private void dataGridViewContacter_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.dataGridViewContacter_DoubleClick(this, null);
            }
        }
    }
}
